package com.example.admin.goodnews.fragment.ShengHuoChangYong;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.ShouJiHaoDataEntity;
import com.example.admin.goodnews.entity.YanZhengDataEntity;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号归属地查询
 * Created by admin on 2016/10/15.
 */

public class ShouJiHaoChaXunFragment extends Fragment {
    private TextInputLayout til_phone_shoujihao;//
    private EditText et_phone_shoujihao;// 获得输入的手机号
    private Button btn_find_shoujihao;// 点击查询手机号归属地
    private Button btn_reset_shoujihao;// 点击清空
    private TextView tv_find_number;// 查询的手机号
    private TextView tv_find_place;// 归属地，格式：省份-市区
    private TextView tv_find_yunyingshang;// 运营商
    private TextView tv_find_error;// 手机号错误信息
    private ShouJiHaoDataEntity shouJiHaoDataEntity;// 数据实体类
    String time = GetCurrentTimeUtil.getCurrentTime();// 获取当前系统时间
    boolean isMobile = false;
    int type = 1;
    String phone = "";
    String prov = "";
    String city = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_shoujihao, null);

        til_phone_shoujihao = (TextInputLayout) view.findViewById(R.id.til_phone_shoujihao);
        et_phone_shoujihao = til_phone_shoujihao.getEditText();
        til_phone_shoujihao.setHint("请输入手机号");

        TextChange textChange = new TextChange();
        et_phone_shoujihao.addTextChangedListener(textChange);

        btn_find_shoujihao = (Button) view.findViewById(R.id.btn_find_shoujihao);
        btn_reset_shoujihao = (Button) view.findViewById(R.id.btn_reset_shoujihao);
        tv_find_number = (TextView) view.findViewById(R.id.tv_find_number);
        tv_find_place = (TextView) view.findViewById(R.id.tv_find_place);
        tv_find_yunyingshang = (TextView) view.findViewById(R.id.tv_find_yunyingshang);
        tv_find_error = (TextView) view.findViewById(R.id.tv_find_error);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 按钮点击事件
        btn_find_shoujihao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMobile(phone)) {
                    tv_find_error.setVisibility(View.INVISIBLE);
                    getData();
                } else {
                    notPhone();
                }
            }
        });
        btn_reset_shoujihao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_phone_shoujihao.setText("");
                tv_find_number.setText("查询的手机号：");
                tv_find_place.setText("号码归属地：");
                tv_find_yunyingshang.setText("运营商：");
                tv_find_error.setVisibility(View.INVISIBLE);
            }
        });
    }

    // 判断输入的内容是否是真实的手机号

    public static boolean isMobile(String phoneNumber) {
        boolean isValid = false;
        CharSequence inputStr = phoneNumber;
        //正则表达式
        String phone = "^1[34578]\\d{9}$";
        Pattern pattern = Pattern.compile(phone);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    // 输入的内容是一个手机号时
    private void getData() {
        String param = NetWorkDataUrl.SHOUJIHAOGUISHUDI_PARAM1 + phone + NetWorkDataUrl.SHOUJIHAOGUISHUDI_PARAM2 + time + NetWorkDataUrl.SHOUJIHAOGUISHUDI_PARAM3;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.SHOUJIHAOGUISHUDI_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    shouJiHaoDataEntity = gson.fromJson(result, ShouJiHaoDataEntity.class);
                    if (shouJiHaoDataEntity.getShowapi_res_body() != null) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 输入的内容不是一个手机号
    private void notPhone() {
        tv_find_number.setText("查询的手机号：" + phone);
        tv_find_place.setText("号码归属地：" + "未知");
        tv_find_yunyingshang.setText("运营商：" + "未知");
        tv_find_error.setVisibility(View.VISIBLE);
    }

    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            phone = et_phone_shoujihao.getText().toString();
            if (phone.length() == 11) {
                btn_find_shoujihao.setEnabled(true);
            } else {
                btn_find_shoujihao.setEnabled(false);
            }
            if (phone.length() > 0) {
                btn_reset_shoujihao.setEnabled(true);
            } else {
                btn_reset_shoujihao.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            phone = et_phone_shoujihao.getText().toString();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    tv_find_number.setText("查询的手机号：" + phone);
                    tv_find_place.setText("号码归属地：" + shouJiHaoDataEntity.getShowapi_res_body().getProv() + "-" + shouJiHaoDataEntity.getShowapi_res_body().getCity());
                    switch (shouJiHaoDataEntity.getShowapi_res_body().getType()) {
                        case 1:
                            tv_find_yunyingshang.setText("运营商：" + "中国移动");
                            break;
                        case 2:
                            tv_find_yunyingshang.setText("运营商：" + "中国电信");
                            break;
                        case 3:
                            tv_find_yunyingshang.setText("运营商：" + "中国联通");
                            break;
                    }
                    break;
            }
        }
    };

}
